﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperSocket.WebSocket.SubProtocol;
using SuperSocket.WebSocket;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

using XBJCYGC;
using System.IO;
using System.Data;
using System.IO.Compression;
using System.Web.Http;
using System.Reflection;

namespace SocketData
{

    public class BroadCast : JsonSubCommand<JObject>
    {
        protected override void ExecuteJsonCommand(WebSocketSession session, JObject commandInfo)
        {
            //遍历RoomDiction，把commandInfo原样发出去
            try
            {
                var room = commandInfo["_room"].ToString();
                var ws = ((MyWS)session.AppServer);
                HashSet<string> sessionList = null;
                if (!ws.RoomDiction.TryGetValue(room, out sessionList))
                {
                    return;
                };
                string sendMsg = commandInfo.ToString(Formatting.None, new JsonConverter[1] { new ExpandoObjectConverter() });
                foreach (var sid in sessionList)
                {
                    var sendSession = ws.GetSessionByID(sid);
                    sendSession.Send(sendMsg);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                commandInfo.Add("_exception", ex.Message);
                session.Send(commandInfo.ToString(Formatting.None, new JsonConverter[1] { new ExpandoObjectConverter() }));
            }
        }
    }
}
